﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HwFace
{
    public partial class HwFaceMain : Form
    {

        HWATT390DataSetTableAdapters.KQZ_EmployeeTableAdapter empadp = new HWATT390DataSetTableAdapters.KQZ_EmployeeTableAdapter();
        HWATT390DataSetTableAdapters.KQZ_Staff_AdditionTableAdapter addadp = new HWATT390DataSetTableAdapters.KQZ_Staff_AdditionTableAdapter();
        public HwFaceMain()
        {
            InitializeComponent();
        }

        private void HwFaceMain_Load(object sender, EventArgs e)
        {
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int iEmployeeID=-1;
            int iEmployeeID_n=-1;
            string sEmployeeCode="";
            string sTitle = "";
            DataTable dt_All_EmployeeID_OnlyMax = new DataTable();
            dt_All_EmployeeID_OnlyMax = empadp.GetDataBy_EmployeeID_OnlyMax();

            for (int i = 0; i < dt_All_EmployeeID_OnlyMax.Rows.Count; i++)
            {
                iEmployeeID = int.Parse(dt_All_EmployeeID_OnlyMax.Rows[i]["EmployeeID"].ToString());
                sEmployeeCode = dt_All_EmployeeID_OnlyMax.Rows[i]["EmployeeCode"].ToString();
                //sTitle = dt_All_EmployeeID_OnlyMax.Rows[i]["Title"].ToString();

                DataTable dt_for_EmployeeCode = new DataTable();
                dt_for_EmployeeCode = empadp.GetDataBy_EmployeeCode(sEmployeeCode);

                //这里循环多条记录，将数据的场所信息写入子表
                short iFIELD_ID = 0;
                for (int j = 0; j < dt_for_EmployeeCode.Rows.Count; j++)
                {                    
                    iEmployeeID_n =int.Parse(dt_for_EmployeeCode.Rows[j]["EmployeeID"].ToString());
                    sTitle = dt_for_EmployeeCode.Rows[j]["Title"].ToString();
                    
                    if (iEmployeeID == iEmployeeID_n)
                    {
                        //主场所                        
                        iFIELD_ID = 3;                      
                    }
                    else
                    {
                        //依次进入附场所
                        iFIELD_ID = short.Parse((j + 4).ToString());                         
                    }

                    //先判断有没有，有的话，更新，没有的话插入
                    DataTable dt_temp = addadp.GetDataBy_ADD_FIELD_ID_DETAILS(short.Parse(iEmployeeID.ToString()), iFIELD_ID + "");
                    if (dt_temp.Rows.Count > 0)
                    {
                        addadp.UpdateQuery_ADD_FIELD_DETAILS(sTitle, iEmployeeID, iFIELD_ID);
                    }
                    else
                    {
                        addadp.Insert(iEmployeeID, iFIELD_ID, sTitle);
                    }  
                }
            }
            MessageBox.Show("操作完成");
        }
    }
}
